#region Header

// MS codename Velocity Distributed Cache Extensions.
// (c) 2009

#endregion


#region Usings

using System;

#endregion


namespace Caching.Extensions.Core
{
    ///<summary>
    /// Opearation on local cache item
    ///</summary>
    [Serializable]
    public enum LocalCacheItemOperation
    {
        /// <summary>
        /// Item found in local cache
        /// </summary>
        Hit,
        /// <summary>
        /// Item not found in local cache
        /// </summary>
        Miss,
        /// <summary>
        /// Item added to local cache
        /// </summary>
        Put,
        /// <summary>
        /// Item removed from local cache
        /// </summary>
        Remove
    }


    /// <summary>
    /// SmartCache event types
    /// </summary>
    [Serializable]
    public enum SmartCacheItemOperation
    {
        /// <summary>
        /// Item found in remote cache
        /// </summary>
        Hit,
        /// <summary>
        /// Item not found in remote cache
        /// </summary>
        Miss,
        /// <summary>
        /// Item version checked in backing cache - item is not modified
        /// </summary>
        QueryUpdateHit,
        /// <summary>
        /// Item version checked in remote cache - item was modified
        /// </summary>
        QueryUpdateMiss,
        /// <summary>
        /// Item added lo remote cache
        /// </summary>
        Put,
        /// <summary>
        /// Item removed from remote cache (by client)
        /// </summary>
        Remove
    }


    /// <summary>
    /// Region level operation
    /// </summary>
    [Serializable]
    public enum SmartCacheRegionOperation
    {
        /// <summary>
        /// Create region
        /// </summary>
        Create,
        /// <summary>
        /// OnRemove region
        /// </summary>
        Remove
    }


    [Serializable]
    public abstract class SmartCacheNotificationBase
    {
        public DateTime TimestampUtc { get; private set; }


        protected SmartCacheNotificationBase() : this(DateTime.UtcNow)
        {
        }


        protected SmartCacheNotificationBase(DateTime timestampUtc)
        {
            TimestampUtc = timestampUtc;
        }
    }


    [Serializable]
    public class SmartCacheExceptionNotification : SmartCacheNotificationBase
    {
        public Exception Exception { get; private set; }
        public string Message { get; private set; }

        public string CacheName
        {
            get; private set;
        }


        public SmartCacheExceptionNotification(string cacheName, Exception exception, string message)
        {
            CacheName = cacheName;
            Exception = exception;
            Message = message;
        }
    }


    [Serializable]
    public abstract class SmartCacheItemNotificationBase : SmartCacheNotificationBase
    {
        public SmartCacheKey Key { get; private set; }


        protected SmartCacheItemNotificationBase(SmartCacheKey key)
        {
            Key = key;
        }
    }


    [Serializable]
    public class SmartCacheRegionNotification : SmartCacheNotificationBase
    {
        public string CacheName { get; private set; }
        public string RegionName { get; private set; }
        public SmartCacheRegionOperation Operation { get; private set; }


        public SmartCacheRegionNotification(string cacheName, string regionName, SmartCacheRegionOperation operation)
        {
            CacheName = cacheName;
            RegionName = regionName;
            Operation = operation;
        }
    }


    [Serializable]
    public class SmartCacheCleanupNotification : SmartCacheNotificationBase
    {
        public int RemovedItemsCount { get; private set; }
        public TimeSpan CleanupTime { get; private set; }


        public SmartCacheCleanupNotification(int removedItemsCount, TimeSpan cleanupTime)
        {
            RemovedItemsCount = removedItemsCount;
            CleanupTime = cleanupTime;
        }
    }


    [Serializable]
    public class SmartCacheLocalItemNotification : SmartCacheItemNotificationBase
    {
        public LocalCacheItemOperation Operation { get; private set; }


        public SmartCacheLocalItemNotification(SmartCacheKey key, LocalCacheItemOperation operation) : base(key)
        {
            Operation = operation;
        }
    }


    [Serializable]
    public class SmartCacheRemoteItemNotification : SmartCacheItemNotificationBase
    {
        public SmartCacheItemOperation Operation { get; private set; }


        public SmartCacheRemoteItemNotification(SmartCacheKey key, SmartCacheItemOperation operation) : base(key)
        {
            Operation = operation;
        }
    }


    /// <summary>
    /// SmartCache notification event sink
    /// </summary>
    public interface ISmartCacheEventSink
    {
        void LocalCacheError(object localCache, SmartCacheExceptionNotification notification);

        void RemoteCacheError(object remoteCache, SmartCacheExceptionNotification notification);


        void LocalCacheItemNotification(object localCache, SmartCacheLocalItemNotification notification);


        /// <summary>
        /// Item level operation
        /// </summary>
        void RemoteCacheItemOperation(object remoteCache, SmartCacheRemoteItemNotification notification);


        /// <summary>
        /// Region level operation
        /// </summary>
        void RegionOperation(object remoteCache, SmartCacheRegionNotification notification);


        /// <summary>
        /// Local cache cleanup
        /// </summary>
        void LocalCacheCleanup(object localCache, SmartCacheCleanupNotification info);
    }
}